var login = '';
var pswd = '';
function make_base_auth(user, password) {
    var tok = user + ':' + password;
    var hash = btoa(tok);
    return "Basic " + hash;
}

function performQuery(url, type, callback, request, async) {
    //            var username = $('#username').val();
    //            var password = $('#password').val();
    $.ajax
        ({
            type:type,
            url:url,
            dataType:'json',
            data:request,
            async:async,
            beforeSend:function (xhr) {
                xhr.setRequestHeader('Authorization', make_base_auth(login, pswd));
            },
            success:callback
        });
}

function authorizeUser() {
    login = $('#username').val();
    pswd = $('#password').val();
    performQuery("user/authorise", "GET", function (data, textStatus, jqXHR) {
            if (data.status == 'ERROR') {
                alert('Error: ' + data.description);
            } else {
                $('#mainForm').css('display', 'block');
                $('#authDiv').css('display', 'none');
                getMessages();
                timer = new RecurringTimer(function () {
                    getLast();
                }, 1000);
                onlineUsers();
                usersTimer = new RecurringTimer(function () {
                    onlineUsers();
                }, 3000);

            }
        }
    );
}
